home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 May / may_2001.iso / intercd / root / Multimedia / ^DivX_Article / DivX / VFW4048src / src / enc_engine.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-21  |  4.5 KB  |  140 lines

  1.  
  2. /**************************************************************************
  3.  *                                                                        *
  4.  * This code is developed by Adam Li.  This software is an                *
  5.  * implementation of a part of one or more MPEG-4 Video tools as          *
  6.  * specified in ISO/IEC 14496-2 standard.  Those intending to use this    *
  7.  * software module in hardware or software products are advised that its  *
  8.  * use may infringe existing patents or copyrights, and any such use      *
  9.  * would be at such party's own risk.  The original developer of this     *
  10.  * software module and his/her company, and subsequent editors and their  *
  11.  * companies (including Project Mayo), will have no liability for use of  *
  12.  * this software or modifications or derivatives thereof.                 *
  13.  *                                                                        *
  14.  * Project Mayo gives users of the Codec a license to this software       *
  15.  * module or modifications thereof for use in hardware or software        *
  16.  * products claiming conformance to the MPEG-4 Video Standard as          *
  17.  * described in the Open DivX license.                                    *
  18.  *                                                                        *
  19.  * The complete Open DivX license can be found at                         *
  20.  * http://www.projectmayo.com/opendivx/license.php .                      *
  21.  *                                                                        *
  22.  **************************************************************************/
  23.  
  24. /************************************************************************
  25.  *
  26.  *  enc_engine.cpp, Encoder Engines
  27.  *
  28.  *  Copyright (C) 2000  DivX Networks
  29.  * 
  30.  *  Adam Li
  31.  *
  32.  *  DivX Advance Research Center <darc@projectmayo.com>
  33.  *
  34.  ************************************************************************/
  35.  
  36. // This file contains the encoder engine functions. There are starting the
  37. // encoder engine, converting the image information to the correct format,
  38. // calling function to compress frames, and ending the encoder engine.
  39.  
  40. #include "stdafx.h"
  41. #include "codec.h"
  42. #include "encore.h"
  43.  
  44. static FILE *bf;
  45.  
  46. long codec::encBegin(LPARAM lParam1, LPARAM lParam2)
  47. {
  48.     BITMAPINFO *lpbiInput;
  49.     BITMAPV4HEADER *infohdr_i;
  50.     ENC_PARAM param1;
  51.  
  52.     lpbiInput = (BITMAPINFO *)lParam1;
  53.     infohdr_i = (BITMAPV4HEADER *)&(lpbiInput->bmiHeader);
  54.  
  55.     param1.x_dim = infohdr_i->bV4Width;
  56.     param1.y_dim = infohdr_i->bV4Height;
  57.     param1.bitrate = bitrate;
  58.     param1.rc_period = rc_period;
  59.     param1.framerate = framerate;
  60.     param1.search_range = search_range;
  61.     param1.max_quantizer = max_quantizer;
  62.     param1.min_quantizer = min_quantizer;
  63.  
  64.     // initialize the encore()
  65.     encore((long)this, ENC_OPT_INIT, ¶m1, NULL);
  66.  
  67.     #ifdef _WRITE_
  68.     bf = fopen("test_alpha4.8.bis", "wb");
  69.     #endif
  70.  
  71.     return ICERR_OK;
  72. }
  73.  
  74. long codec::encEnd(LPARAM lParam1, LPARAM lParam2)
  75. {
  76.     encore((long)this, ENC_OPT_RELEASE, NULL, NULL);
  77.  
  78.     #ifdef _WRITE_
  79.     fclose(bf);
  80.     #endif
  81.  
  82.     return ICERR_OK;
  83. }
  84.  
  85. long codec::encEncode(LPARAM lParam1, LPARAM lParam2)
  86. {
  87.     ICCOMPRESS *icc = (ICCOMPRESS *)lParam1;
  88.     int iccSize = (int)lParam2;
  89.     BITMAPV4HEADER *lpbihdr_i, *lpbihdr_o;
  90.     ENC_FRAME frame;
  91.     ENC_RESULT outflag;
  92.  
  93.     long imgtype, x_dim, y_dim, size, flagmem = 0;
  94.     long enc_opt = 0, result;
  95.  
  96.     lpbihdr_i = (BITMAPV4HEADER *)icc->lpbiInput;
  97.     lpbihdr_o = (BITMAPV4HEADER *)icc->lpbiOutput;
  98.  
  99.     imgtype = getImageType(lpbihdr_i);
  100.     if (imgtype == 0) return ICERR_ERROR;
  101.  
  102.     // call encore()
  103.     x_dim = lpbihdr_i->bV4Width;
  104.     y_dim = lpbihdr_o->bV4Height;
  105.     size = x_dim * y_dim * 3 / 2;
  106.     if ((imgtype == 64) || (imgtype == 65)) {    // planer image
  107.         frame.image = icc->lpInput;
  108.         convertImage(icc->lpInput, frame.image, imgtype, x_dim, y_dim);
  109.     } else {                                    // packed image
  110.         if ((frame.image = malloc(size)) == NULL) return ICERR_ERROR;
  111.         flagmem = 1;
  112.         convertImage(icc->lpInput, frame.image, imgtype, x_dim, y_dim);
  113.     }
  114.  
  115.     frame.bitstream = icc->lpOutput;
  116.     frame.length = 0;
  117.  
  118.     // encode the frame with encore
  119.     result = encore((long)this, 
  120.         enc_opt, 
  121.         &frame,
  122.         &outflag);
  123.  
  124.     // set the length of the output bitstream
  125.     lpbihdr_o->bV4SizeImage = frame.length;
  126.     // set the flag so Windows knows it is a key frame
  127.     *(icc->lpdwFlags) = 0;
  128.     if (outflag.isKeyFrame) 
  129.         *(icc->lpdwFlags) = AVIIF_KEYFRAME;
  130.  
  131.     #ifdef _WRITE_
  132.     fwrite(frame.bitstream, 1, frame.length, bf);
  133.     #endif
  134.  
  135.     if (flagmem) free(frame.image);
  136.  
  137.     return ICERR_OK;
  138. }
  139.  
  140.